package top.jiangqiang.qyblog.core.validate;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

public class AllowValidator implements ConstraintValidator<Allow, Object> {
    private String[] allowValues;

    @Override
    public void initialize(Allow constraintAnnotation) {
        ConstraintValidator.super.initialize(constraintAnnotation);
        allowValues = constraintAnnotation.allowValues();
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        for (String allowValue : allowValues) {
            if (String.valueOf(value).equals(allowValue)) {
                return true;
            }
        }
        return false;
    }
}
